package cn.sh.tools.network;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.ConsolePipeline;
import us.codecraft.webmagic.processor.PageProcessor;

/**
 * @author code4crafter@gmail.com <br>
 * @since 0.5.1
 */
public class ImgPageFirstProcessor implements PageProcessor {

    private Site site = Site.me().setRetryTimes(3).setSleepTime(1000).setTimeOut(10000);

    @Override
    public Site getSite() {
        return site;
    }

    @Override
    public void process(Page page) {
        String url = page.getUrl().get();
        int beginIndex = url.lastIndexOf(ImgPageConstant.LINE) + 1;
        int endIndex = url.lastIndexOf(ImgPageConstant.SPOT);
        int index = Integer.parseInt(url.substring(beginIndex, endIndex));

        for (int i = 0; i < ImgPageConstant.COUNT; i++) {
            url = url.substring(0, beginIndex).concat(String.valueOf(index++)).concat(url.substring(url.lastIndexOf(".")));
            Spider.create(new ImgPageSecondProcessor())
                    .addUrl(url)
                    .addPipeline(new ConsolePipeline())
                    .thread(5)
                    .run();
        }
    }

}
